package com.ybl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ybl.common.BaseContext;
import com.ybl.common.R;
import com.ybl.dto.OrdersDto;
import com.ybl.entity.OrderDetail;
import com.ybl.entity.Orders;
import com.ybl.entity.Seats;
import com.ybl.entity.ShoppingCart;
import com.ybl.service.OrderDetailService;
import com.ybl.service.OrderService;
import com.ybl.service.SeatsService;
import com.ybl.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private SeatsService seatsService;

    /**
     * 用户下单功能
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 后台订单查询的分页
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number != null, Orders::getNumber, number)
                .gt(StringUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime)
                .lt(StringUtils.isNotEmpty(endTime), Orders::getOrderTime, endTime);
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 用户页面的订单
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        //先查询Orders表
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //得到不包括orderDetails的数据
        orderService.page(pageInfo, queryWrapper);
        //根据已查询到的数据，查询orderDetail表
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            LambdaQueryWrapper<OrderDetail> orderDetailQW = new LambdaQueryWrapper<>();
            orderDetailQW.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> list1 = orderDetailService.list(orderDetailQW);
            BeanUtils.copyProperties(item, ordersDto);
            ordersDto.setOrderDetails(list1);
            return ordersDto;
        }).collect(Collectors.toList());
        Page<OrdersDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 更改订单状态
     */
    @PutMapping
    @CacheEvict(value = "seatsCache", allEntries = true)
    public R<String> updateOrderStatus(@RequestBody Orders orders) {
        orderService.updateById(orders);
        if (orders.getStatus() == 4 || orders.getStatus() == 5) {
            LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Orders::getId, orders.getId());
            final Orders one = orderService.getOne(lambdaQueryWrapper);
            final Long seatsId = one.getSeatsId();
            Seats seats = seatsService.getById(seatsId);
            seats.setSystatus(0);
            seatsService.updateById(seats);
        }
        return R.success("订单状态修改成功");
    }
}